package com.xgs.system.mapper;

import java.util.List;
import java.util.Map;

/**
 * @author JerryLu
 * @date 2024/12/9 13:06
 * @description 计费统计执行方法
 */
public class SqlProvider {
    public String dynamicSelect(Map<String, Object> params) {
        String dbName = (String) params.get("dbName");
        String deptId = (String) params.get("deptId");
        String fields = (String) params.get("fields");
        List<String> dates = (List<String>) params.get("dates");

        // 基础SQL模板
        String baseSql = "SELECT ENTOLLSTATIONNAME, EXTOLLSTATIONNAME, ENTIME, EXTIME, EXVEHICLEID, EXVEHICLETYPE, EXITFEETYPE, " + "FEEMILEAGE, PAYFEE, FEE, DISCOUNTFEE FROM %s WHERE BITAND(dealstatusex, 4) = 4 AND paykind <> 110 AND " + "LPAD(TO_CHAR(EXITSTATION), 7, '0') LIKE '%s%%' AND ENSTATIONHEX IN (%s)";
        // 拼接所有日期的SQL
        StringBuilder finalSql = new StringBuilder();
        for (int i = 0; i < dates.size(); i++) {
            String date = dates.get(i);
            String tableName = String.format("dbbase%s.laneexit%s", dbName, date); // 动态表名
            String currentSql = String.format(baseSql, tableName, deptId, fields); // 动态生成SQL

            finalSql.append(currentSql);

            // 如果不是最后一个表，添加 UNION ALL
            if (i < dates.size() - 1) {
                finalSql.append(" UNION ALL ");
            }
        }

        return finalSql.toString();

//        String sql = String.format("SELECT ENTOLLSTATIONNAME, EXTOLLSTATIONNAME, ENTIME, EXTIME, EXVEHICLEID, EXVEHICLETYPE, " +
//                "FEEMILEAGE, PAYFEE, FEE, DISCOUNTFEE FROM %s where BITAND(dealstatusex, 4) = 4 AND paykind <> 110 AND " +
//                "LPAD(TO_CHAR(EXITSTATION), 7, '0') LIKE '%s%%' AND ENSTATIONHEX in (%s)", tableName, deptId, fields);
//        return sql;
    }
}
